﻿Public Class u_NguyenLieu
    Private Sub LoadData()
        Dim nl As New NguyenLieuBUS
        dgvNL.DataSource = nl.GetNguyenLieu
        Dim nhom As New NhomBUS
        cbxNhomHang.DataSource = nhom.GetNhomNL
        cbxNhomHang.DisplayMember = "TenNhomNL"
        cbxNhomHang.ValueMember = "MaNhomNL"
        cbxNhomHang.SelectedIndex = 0
        cbxDonViNL.SelectedItem = 0
        Dim ncc As New NhomBUS
        cbxNhaCC.DataSource = ncc.GetNhomNhaCC
        cbxNhaCC.DisplayMember = "TenNhaCungCap"
        cbxNhaCC.ValueMember = "MaNhaCungCap"
        cbxNhaCC.SelectedIndex = 0
    End Sub

    Private Sub u_NguyenLieu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
    End Sub

    Private Sub btnThemNL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemNL.Click
        Dim newNL As New NguyenLieuDTO
        newNL.MaNL = txtMaNL.Text
        newNL.TenNL = txtTenNL.Text
        newNL.DonVi = cbxDonViNL.SelectedItem
        newNL.SoLuong = Integer.Parse(txtSoLuong.Text.ToString)
        newNL.KhoiLuong = Integer.Parse(txtKhoiLuong.Text.ToString)
        newNL.MaNhom = cbxNhomHang.SelectedValue
        newNL.MaNhaCC = cbxNhaCC.SelectedValue
        Dim nlBUS As New NguyenLieuBUS
        nlBUS.AddNguyenLieu(newNL)
        LoadData()
    End Sub


    Private Sub btnXoaNL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaNL.Click
        If dgvNL.SelectedRows.Count <> 0 Then
            If dgvNL.SelectedRows(0).Index <> dgvNL.RowCount Then
                Dim delNL As New NguyenLieuDTO
                delNL.STT = dgvNL.SelectedRows(0).Cells("STT").Value
                Dim nlBUS As New NguyenLieuBUS
                nlBUS.DelNguyenLieu(delNL)
                LoadData()
            End If
        End If
    End Sub

    Private Sub dgvNL_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvNL.SelectionChanged
        If dgvNL.SelectedRows.Count <> 0 Then
            If dgvNL.SelectedRows(0).Index <> dgvNL.RowCount Then
                txtMaNL.Text = dgvNL.SelectedRows(0).Cells("MaNL").Value
                txtTenNL.Text = dgvNL.SelectedRows(0).Cells("TenNL").Value
                txtKhoiLuong.Text = dgvNL.SelectedRows(0).Cells("KhoiLuong").Value
                txtSoLuong.Text = dgvNL.SelectedRows(0).Cells("SoLuong").Value
                cbxDonViNL.Text = dgvNL.SelectedRows(0).Cells("DonVi").Value
                cbxNhomHang.SelectedValue = dgvNL.SelectedRows(0).Cells("MaNhomNL").Value
                cbxNhaCC.SelectedValue = dgvNL.SelectedRows(0).Cells("MaNhaCungCap").Value
            End If
        End If
    End Sub

    Private Sub btnCapNhapNL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhapNL.Click
        If dgvNL.SelectedRows.Count <> 0 Then
            If dgvNL.SelectedRows(0).Index <> dgvNL.RowCount Then
                Dim newNL As New NguyenLieuDTO
                newNL.STT = dgvNL.SelectedRows(0).Cells("STT").Value
                newNL.MaNL = txtMaNL.Text
                newNL.TenNL = txtTenNL.Text
                newNL.DonVi = cbxDonViNL.SelectedValue
                newNL.MaNhaCC = cbxNhaCC.SelectedValue
                newNL.KhoiLuong = txtKhoiLuong.Text
                newNL.SoLuong = txtSoLuong.Text
                newNL.MaNhom = cbxNhomHang.SelectedValue
                Dim nlBUS As New NguyenLieuBUS
                nlBUS.UpdateNguyenLieu(newNL)
                LoadData()
            End If
        End If
    End Sub
End Class
